package com.lifeverse.factory.type;

import com.lifeverse.entity.LifeEntity;
import com.lifeverse.entity.enums.LifeEntityType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 个人生命体创建器
 * 专门创建个人类型的生命体
 */
@Slf4j
@Component
public class IndividualLifeEntityCreator implements LifeEntityCreator {
    
    @Override
    public LifeEntity create(String name, String description, Map<String, Object> configuration) {
        log.info("Creating individual life entity: {}", name);
        
        LifeEntity entity = new LifeEntity();
        entity.setType(LifeEntityType.INDIVIDUAL);
        
        // 设置个人生命体特有的初始属性
        setupIndividualAttributes(entity, configuration);
        
        return entity;
    }
    
    @Override
    public LifeEntity clone(LifeEntity originalEntity, String newName, double variationLevel) {
        log.info("Cloning individual life entity: {} to {}", originalEntity.getName(), newName);
        
        LifeEntity clonedEntity = new LifeEntity();
        clonedEntity.setType(LifeEntityType.INDIVIDUAL);
        
        // 复制原始个人的属性
        copyIndividualAttributes(clonedEntity, originalEntity);
        
        return clonedEntity;
    }
    
    @Override
    public boolean validateConfiguration(Map<String, Object> configuration) {
        if (configuration == null) {
            return true; // 允许空配置，使用默认值
        }
        
        // 验证个人特有配置
        if (configuration.containsKey("age")) {
            Object age = configuration.get("age");
            if (!(age instanceof Number) || ((Number) age).intValue() < 0) {
                return false;
            }
        }
        
        if (configuration.containsKey("experienceLevel")) {
            Object experience = configuration.get("experienceLevel");
            if (!(experience instanceof String)) {
                return false;
            }
            String level = (String) experience;
            if (!level.matches("(?i)(beginner|intermediate|advanced|expert|master)")) {
                return false;
            }
        }
        
        if (configuration.containsKey("personalityType")) {
            Object personality = configuration.get("personalityType");
            if (!(personality instanceof String)) {
                return false;
            }
        }
        
        return true;
    }
    
    @Override
    public Map<String, Object> getDefaultConfiguration() {
        return Map.of(
                "age", 30,
                "experienceLevel", "Intermediate",
                "personalityType", "Balanced",
                "learningStyle", "Visual",
                "motivationType", "Achievement",
                "socialPreference", "Moderate",
                "riskTolerance", "Medium",
                "adaptabilityScore", 70.0,
                "emotionalIntelligence", 65.0,
                "technicalSkills", 60.0
        );
    }
    
    /**
     * 设置个人生命体特有属性
     */
    private void setupIndividualAttributes(LifeEntity entity, Map<String, Object> configuration) {
        Map<String, Object> config = configuration != null ? configuration : getDefaultConfiguration();
        
        // 根据年龄设置不同的初始能力值
        int age = ((Number) config.getOrDefault("age", 30)).intValue();
        setupAgeBasedAttributes(entity, age);
        
        // 根据经验水平调整属性
        String experienceLevel = (String) config.getOrDefault("experienceLevel", "Intermediate");
        adjustAttributesByExperience(entity, experienceLevel);
        
        // 根据性格类型调整属性
        String personalityType = (String) config.getOrDefault("personalityType", "Balanced");
        adjustAttributesByPersonality(entity, personalityType);
        
        // 根据学习风格调整创造力
        String learningStyle = (String) config.getOrDefault("learningStyle", "Visual");
        adjustCreativityByLearningStyle(entity, learningStyle);
        
        // 个人生命体通常具有较高的适应性和学习能力
        entity.setEnergyLevel(BigDecimal.valueOf(85.0));
        entity.setHealthScore(BigDecimal.valueOf(80.0));
    }
    
    /**
     * 根据年龄设置属性
     */
    private void setupAgeBasedAttributes(LifeEntity entity, int age) {
        if (age < 18) {
            // 青少年：高能量，高创造力，低智慧
            entity.setCreativityScore(BigDecimal.valueOf(80.0));
            entity.setWisdomScore(BigDecimal.valueOf(30.0));
            entity.setCollaborationScore(BigDecimal.valueOf(60.0));
            entity.setEnergyLevel(BigDecimal.valueOf(95.0));
        } else if (age < 30) {
            // 青年：平衡发展，学习能力强
            entity.setCreativityScore(BigDecimal.valueOf(75.0));
            entity.setWisdomScore(BigDecimal.valueOf(50.0));
            entity.setCollaborationScore(BigDecimal.valueOf(70.0));
            entity.setEnergyLevel(BigDecimal.valueOf(90.0));
        } else if (age < 50) {
            // 中年：智慧和经验积累，创造力稳定
            entity.setCreativityScore(BigDecimal.valueOf(70.0));
            entity.setWisdomScore(BigDecimal.valueOf(80.0));
            entity.setCollaborationScore(BigDecimal.valueOf(85.0));
            entity.setEnergyLevel(BigDecimal.valueOf(80.0));
        } else if (age < 65) {
            // 中老年：高智慧，丰富经验，协作能力强
            entity.setCreativityScore(BigDecimal.valueOf(65.0));
            entity.setWisdomScore(BigDecimal.valueOf(95.0));
            entity.setCollaborationScore(BigDecimal.valueOf(90.0));
            entity.setEnergyLevel(BigDecimal.valueOf(70.0));
        } else {
            // 老年：极高智慧，经验丰富，但能量较低
            entity.setCreativityScore(BigDecimal.valueOf(60.0));
            entity.setWisdomScore(BigDecimal.valueOf(100.0));
            entity.setCollaborationScore(BigDecimal.valueOf(95.0));
            entity.setEnergyLevel(BigDecimal.valueOf(60.0));
        }
    }
    
    /**
     * 根据经验水平调整属性
     */
    private void adjustAttributesByExperience(LifeEntity entity, String experienceLevel) {
        BigDecimal wisdomMultiplier;
        BigDecimal collaborationMultiplier;
        
        switch (experienceLevel.toLowerCase()) {
            case "beginner" -> {
                wisdomMultiplier = BigDecimal.valueOf(0.7);
                collaborationMultiplier = BigDecimal.valueOf(0.8);
            }
            case "intermediate" -> {
                wisdomMultiplier = BigDecimal.valueOf(1.0);
                collaborationMultiplier = BigDecimal.valueOf(1.0);
            }
            case "advanced" -> {
                wisdomMultiplier = BigDecimal.valueOf(1.3);
                collaborationMultiplier = BigDecimal.valueOf(1.2);
            }
            case "expert" -> {
                wisdomMultiplier = BigDecimal.valueOf(1.5);
                collaborationMultiplier = BigDecimal.valueOf(1.4);
            }
            case "master" -> {
                wisdomMultiplier = BigDecimal.valueOf(1.8);
                collaborationMultiplier = BigDecimal.valueOf(1.6);
            }
            default -> {
                wisdomMultiplier = BigDecimal.valueOf(1.0);
                collaborationMultiplier = BigDecimal.valueOf(1.0);
            }
        }
        
        entity.setWisdomScore(entity.getWisdomScore().multiply(wisdomMultiplier));
        entity.setCollaborationScore(entity.getCollaborationScore().multiply(collaborationMultiplier));
    }
    
    /**
     * 根据性格类型调整属性
     */
    private void adjustAttributesByPersonality(LifeEntity entity, String personalityType) {
        switch (personalityType.toLowerCase()) {
            case "creative" -> {
                entity.setCreativityScore(entity.getCreativityScore().multiply(BigDecimal.valueOf(1.4)));
                entity.setWisdomScore(entity.getWisdomScore().multiply(BigDecimal.valueOf(0.9)));
                entity.setCollaborationScore(entity.getCollaborationScore().multiply(BigDecimal.valueOf(1.1)));
            }
            case "analytical" -> {
                entity.setCreativityScore(entity.getCreativityScore().multiply(BigDecimal.valueOf(0.8)));
                entity.setWisdomScore(entity.getWisdomScore().multiply(BigDecimal.valueOf(1.4)));
                entity.setCollaborationScore(entity.getCollaborationScore().multiply(BigDecimal.valueOf(0.9)));
            }
            case "social" -> {
                entity.setCreativityScore(entity.getCreativityScore().multiply(BigDecimal.valueOf(1.1)));
                entity.setWisdomScore(entity.getWisdomScore().multiply(BigDecimal.valueOf(1.1)));
                entity.setCollaborationScore(entity.getCollaborationScore().multiply(BigDecimal.valueOf(1.5)));
            }
            case "leader" -> {
                entity.setCreativityScore(entity.getCreativityScore().multiply(BigDecimal.valueOf(1.2)));
                entity.setWisdomScore(entity.getWisdomScore().multiply(BigDecimal.valueOf(1.3)));
                entity.setCollaborationScore(entity.getCollaborationScore().multiply(BigDecimal.valueOf(1.4)));
            }
            case "introvert" -> {
                entity.setCreativityScore(entity.getCreativityScore().multiply(BigDecimal.valueOf(1.3)));
                entity.setWisdomScore(entity.getWisdomScore().multiply(BigDecimal.valueOf(1.2)));
                entity.setCollaborationScore(entity.getCollaborationScore().multiply(BigDecimal.valueOf(0.7)));
            }
            case "extrovert" -> {
                entity.setCreativityScore(entity.getCreativityScore().multiply(BigDecimal.valueOf(1.1)));
                entity.setWisdomScore(entity.getWisdomScore().multiply(BigDecimal.valueOf(0.9)));
                entity.setCollaborationScore(entity.getCollaborationScore().multiply(BigDecimal.valueOf(1.4)));
            }
            case "balanced" -> {
                // 保持默认值，不做调整
            }
            default -> {
                // 未知类型，保持默认值
            }
        }
    }
    
    /**
     * 根据学习风格调整创造力
     */
    private void adjustCreativityByLearningStyle(LifeEntity entity, String learningStyle) {
        BigDecimal creativityMultiplier = switch (learningStyle.toLowerCase()) {
            case "visual" -> BigDecimal.valueOf(1.2);
            case "auditory" -> BigDecimal.valueOf(1.1);
            case "kinesthetic" -> BigDecimal.valueOf(1.3);
            case "reading" -> BigDecimal.valueOf(1.0);
            case "multimodal" -> BigDecimal.valueOf(1.4);
            default -> BigDecimal.valueOf(1.0);
        };
        
        entity.setCreativityScore(entity.getCreativityScore().multiply(creativityMultiplier));
    }
    
    /**
     * 复制个人属性
     */
    private void copyIndividualAttributes(LifeEntity clonedEntity, LifeEntity originalEntity) {
        // 复制基本属性
        clonedEntity.setWisdomScore(originalEntity.getWisdomScore());
        clonedEntity.setCreativityScore(originalEntity.getCreativityScore());
        clonedEntity.setCollaborationScore(originalEntity.getCollaborationScore());
        clonedEntity.setEnergyLevel(originalEntity.getEnergyLevel());
        clonedEntity.setHealthScore(originalEntity.getHealthScore());
        
        // 复制配置信息
        clonedEntity.setConfiguration(originalEntity.getConfiguration());
    }
}